package jxkjsync

type jobInfo struct {
	dataType string
	sql      string
	srv      SyncService
}
type Logger interface {
	Warnf(string, ...interface{})
	Error(...interface{})
	Errorf(string, ...interface{})
	Infof(string, ...interface{})
	Info(...interface{})
	Fatalf(string, ...interface{})
	Fatal(...interface{})
}
type WorkerWindow interface {
	ShowReading()
	ShowRunning()
	ShowStopping()
	ConfirmBox(string) int
	NotifyInfo(string)
	ShowRunInfo(string)
}

type QuickWorker interface {
	DoStart(switchMap map[string]bool, sqlMap map[string]string)
	DoStop(err error)
	IsRun() bool
	//DoSave(cfgType string, content interface{}, dataType string)
	DoAwake(switchMap map[string]bool, sqlMap map[string]string)
	Error() error
	SetBeforeStart(func() error)
	SetExceptionHandle(f func(err error))
	SetAfterStop(func() error)
	SetJobFinishHook(func() error)
	setLogger(Logger)
	setPrefixSyncedCache(string)
}
type WorkerType string

const (
	WorkerTypeQueue = "queue"
	WorkerTypeStep  = "Step"
)

func NewQuickStepWorker(mw WorkerWindow, interval int, options ...QuickWorkerOption) QuickWorker {
	return newQuickStepWorker(mw, interval, WorkerTypeStep, options...)
}
func NewQuickQueueWorker(mw WorkerWindow, interval int, options ...QuickWorkerOption) QuickWorker {
	return newQuickStepWorker(mw, interval, WorkerTypeQueue, options...)
}
func newQuickStepWorker(mw WorkerWindow, interval int, workerType WorkerType, options ...QuickWorkerOption) QuickWorker {
	var (
		wk              QuickWorker
		defaultInterval = 120
	)
	if interval > 0 {
		defaultInterval = interval
	}
	if workerType == WorkerTypeQueue {
		wk = &queueWorker{
			mw:                mw,
			interval:          defaultInterval,
			configSuffix:      "yml",
			prefixSyncedCache: "quick_sync_",
		}
	} else {
		wk = &stepWorker{
			mw:                mw,
			interval:          defaultInterval,
			configSuffix:      "yml",
			prefixSyncedCache: "quick_sync_",
		}
	}

	// 应用选项函数
	for _, option := range options {
		option(wk)
	}
	return wk
}

type QuickWorkerOption func(QuickWorker)

func WithLogger(logger Logger) QuickWorkerOption {
	return func(w QuickWorker) {
		w.setLogger(logger)
	}
}
func WithCachePrefix(prefixSyncedCache string) QuickWorkerOption {
	return func(w QuickWorker) {
		w.setPrefixSyncedCache(prefixSyncedCache)
	}
}

//	func WithConfigSuffix(configSuffix string) QuickWorkerOption {
//		return func(w *queueWorker) {
//			w.configSuffix = configSuffix
//		}
//	}
